/**
 *  Copyright (c)  2010  Angel Ortiz.
 *  Permission is granted to copy, distribute and/or modify this document
 *  under the terms of the GNU Free Documentation License, Version 1.3
 *  or any later version published by the Free Software Foundation;
 *  with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts.
 *  A copy of the license is included in the section entitled "GNU
 *  Free Documentation License". 
 */
import javax.swing.JOptionPane;
import javax.swing.table.AbstractTableModel;

/**
 * This class defines table model object that controls the data
 * 
 * @author angel
 * @date 02/12/2010
 */
public class TableModelObj extends AbstractTableModel {
	private Object[] columnNames = null;
	private Object[][] rowData = null;

	/**
	 * Default constructor
	 */
	public TableModelObj() {
		super();
	}

	/**
	 * Alternative constructor that initializes the columnNames
	 * 
	 * @param columnNames
	 *            - the names of the columns
	 */
	public TableModelObj(Object[] columnNames, Object[][] data) {
		this();
		this.columnNames = columnNames;
		this.rowData = data;
	}

	/**
	 * Sets the columns names using the parameter name
	 * 
	 * @param names
	 *            - the names of the columns
	 */
	public void setColumnNames(Object[] names) {
		columnNames = names;
	}

	/**
	 * Sets the table data using the parameter data
	 * 
	 * @param data
	 *            - the data object of the table
	 */
	public void setData(Object[][] data) {
		rowData = data;
	}

	/**
	 * Returns the name of the column at the location pointed by index
	 * 
	 * @param index
	 *            - the index of the column object.
	 * @return a String with the name.
	 */
	public String getColumnName(int index) {
		return columnNames[index].toString();
	}

	/**
	 * Returns the value of the location determined
	 * 
	 * @param row
	 *            - the row of the data object
	 * @param col
	 *            - the column of the data object
	 * @return an Object with the value.
	 */
	public Object getValueAt(int row, int col) {
		return rowData[row][col];
	}

	/**
	 * Checks if the Cell is editable
	 * 
	 * @param row
	 *            - the row of the data object
	 * @param col
	 *            - the column of the data object
	 * @return a boolean with editable status
	 */
	public boolean isCellEditable(int row, int col) {
		if (col < 9) {
			return false;
		}
		return true;
	}

	/**
	 * Loads a object value to the data object at a location
	 * 
	 * @param obj
	 *            - the object to load
	 * @param row
	 *            - the row of the data object
	 * @param col
	 *            - the column of the data object
	 */
	public void setValueAt(Object obj, int row, int col) {
		if (col == 9) {
			try {
				int value = Integer.parseInt(obj.toString());
				rowData[row][col] = obj;
				fireTableCellUpdated(row, col);
			} catch (NumberFormatException nfe) {
				// String message = "Error " + "\"" + obj.toString() + "\"" +
				// " is not in Integer Formant";
				// JOptionPane.showMessageDialog(null, message, "Error Message",
				// JOptionPane.ERROR_MESSAGE);
			}
		} else
			rowData[row][col] = obj;
		fireTableCellUpdated(row, col);
	}

	/**
	 * Returns the index of the object for columns names.
	 * 
	 * @return the index of the column's names
	 */
	public int getColumnCount() {
		return columnNames.length;
	}

	/**
	 * Returns the index of the rows from the data Object.
	 * 
	 * @return the index of data object
	 */
	public int getRowCount() {
		return rowData.length;
	}

	/**
	 * Returns the rowData Object of the table
	 * 
	 * @return the table's data object
	 */
	public Object[][] getDataObject() {
		return this.rowData;
	}
}
